package map;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.FloatMath;

import java.util.Objects;

/**
 * Created by 佳星 on 08-09-0009.
 */
public class MapPoint {

    public final int color;
    public final String tag;

    private MapWidget widget;
    private int alaph =  255;
    private PointF position = new PointF();
    private float radius = 10f;
    private final Paint drawPaint;
    private static final Paint cleanPaint;


    static {
        cleanPaint = new Paint();
        cleanPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }

    public MapPoint(String obj, int color) {
        this.tag   = obj;
        this.color = color;

        drawPaint = new Paint();
        drawPaint.setColor(color);
        drawPaint.setStyle(Paint.Style.FILL);
        drawPaint.setAntiAlias(true);
        drawPaint.setFilterBitmap(true);
    }

    public void onDraw(Canvas canvas){
        draw(canvas);
        if (widget != null){
            widget.showAtPoint(this);
        }
    }

    public void draw(Canvas canvas){
        drawPaint.setAlpha(alaph);
        canvas.drawCircle(position.x, position.y, radius, drawPaint);
    }

    public void setAlaph(int alaph){
        this.alaph = alaph;
    }
    public void setWidget(MapWidget widget){
        this.widget = widget;
    }

    public void cleanWidget() {
        this.widget = null;
    }

    public MapWidget getWidget() {
        return widget;
    }

    public void setPosition(float x, float y){
        position.set(x, y);
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public void onScale(float scale, float currX, float currY) {
        scaleBy(scale, currX, currY);
        if (widget != null){
            widget.showAtPoint(this);
        }
    }
    public void onScale(float scale) {
        position.x *= scale;
        position.y *= scale;
    }

    public void scaleBy(float scale, float centerX, float centerY){
        PointF newPosition = scaleByPoint(position.x, position.y, centerX, centerY, scale);
        position.set(newPosition);
    }

    /**
     * 获取缩放后新的中心坐标
     * @param targetPointX
     * @param targetPointY
     * @param scaleCenterX
     * @param scaleCenterY
     * @param scale
     * @return
     */
    private PointF scaleByPoint(float targetPointX,float targetPointY,float scaleCenterX,float scaleCenterY,float scale){
        Matrix matrix = new Matrix();
        matrix.preTranslate(targetPointX,targetPointY);
        matrix.postScale(scale,scale,scaleCenterX,scaleCenterY);
        float[] values = new float[9];
        matrix.getValues(values);
        return new PointF(values[Matrix.MTRANS_X],values[Matrix.MTRANS_Y]);
    }

    public final void onTranslate(float distX, float distY){
        translate(distX, distY);
        if (widget != null){
            widget.showAtPoint(this);
        }
    }

    public void translate(float distX, float distY){
        position.x += distX;
        position.y += distY;
    }


    public PointF getCenter() {
        return position;
    }

    public boolean inArea(float x, float y){
        boolean ret = false;
        float dx = position.x - Math.abs(x);
        float dy = position.y - Math.abs(y);
        float d  = FloatMath.sqrt(dx*dx + dy*dy);

        if (d <= radius+10){
            ret = true;
        }

        return ret;
    }

}
